#include <iostream>
#include <cstdlib>
#include <time.h>

#include "application.h"
//include "boardstate.h"

using namespace std;

int main()
{


    try {

        Application app(1024, 768);
        app.run();

    } catch (exception& e) {
        std::cout << "Exception occured!\n" << e.what() << "\n";
    }


/*

    //***************** SIMPLE TEST ***********************
    BoardState p;
    Move moveList[256];
    Move mOld;
    int countMoves = 0;
    int turn = 1;
    int depth = 0;
    int maxTurns = 500;
    string input;
    bool wAI = false;
    bool bAI = false;
    bool cls = false;
    string buffer;

    // settigs
    p.clear();
    p.initPos();
    p.setTurn(WHITE);
    srand ( time(NULL) );
    system("cls");

    cout << "SimpleChess (enter Q to quit)\n\n";
    cout << "White player: human or computer (h / c): ";
    cin >> input;
    if (input == "c"){
        wAI = true;
    }
    cout << "Black player: human or computer (h / c): ";
    cin >> input;
    if (input == "c"){
        bAI = true;
    }
    cout << "Clear the screen after each turn (y / n): ";
    cin >> input;
    if (input == "y"){
        cls = true;
    }
    if (wAI || bAI){
        cout << "Give AI calculate depth: ";
        cin >> depth;
    }
    if (wAI && bAI){
        cout << "Max turn limit: ";
        cin >> maxTurns;
    }
    system("cls");


    do{
        // print the game board
        if (cls){
            system("cls");
        }
        cout << "\n\nTURN " << turn << "\n";
        p.print();

        // gen legal moves
        countMoves = p.genLegalMoves(moveList);

        // print last move
        if (turn != 1){
            std::cout << "\nLast move: ";
            mOld.print();
            std::cout << "\n";
        }

        // print legal moves
        std::cout << "\nLegal moves: " << countMoves << "\n\n";
        for (int i = 0 ; i < countMoves ; ++i){
            moveList[i].print();
            if ((i+1)%5 == 0 && i != countMoves-1){
                std::cout << std::endl;
            } else {
                std::cout << "\t";
            }
        }

        // check for loser
        if (countMoves == 0){
            if (p.getTurn() == WHITE){
                cout << "\n\nCHECKMATE - BLACK did win the game\n";
            } else {
                cout << "\n\nCHECKMATE - WHITE did win the game\n";
            }
            input = "q";
        } else if (turn > maxTurns){
            cout << "\n\nMax turn limit reached\n";
            input = "q";
        }

        // make the move
        if ((p.getTurn() == WHITE && wAI == true)){
            mOld = p.genBestMove(depth);
            p.execMove(mOld);
        } else if (p.getTurn() == BLACK && bAI == true){
            mOld = p.genBestMove(depth);
            p.execMove(mOld);
        } else {
            // handle the input
            Move m;
            bool valid = false;
            while (!valid && input != "Q" && input != "q"){
                if(p.getTurn() == WHITE) {
                    cout << "\n\nWHITE, give your move: ";
                } else {
                    cout << "\n\nBLACK, give your move: ";
                }
                cin >> input;
                m.strToMove(input.c_str());

               for (int i=0; i< countMoves; ++i){
                    if (moveList[i].getX1() == m.getX1() &&
                        moveList[i].getX2() == m.getX2() &&
                        moveList[i].getY1() == m.getY1() &&
                        moveList[i].getY2() == m.getY2() &&
                        moveList[i].getSpecialRule() == m.getSpecialRule()){
                        valid = true;
                    }
                }
            }

            // execute the move if not Quit
            if (input != "Q" && input != "q"){
                m.strToMove(input.c_str());
                mOld = m;
                p.execMove(m);
            }
        }

        turn++;
    } while(input != "Q" && input != "q");
    //***************** SIMPLE TEST ***********************
*/
    return 0;
}
